#!/bin/bash
#
# This script starts a Fabric CA server for the organization specified by the first parameter.

source ./utils.sh

# Main script
if [ $# -ne 1 ]; then
    printf "Error: 0 or a number > 1 parameter(s) provided.\nThe only parameter should be provided specifying the organization name.\n"
    exit 1
fi

checkBinAvailability || {
    printf "Error: Fabric CA binaries not available.\n"
    exit 2
}

WORKING_DIR=${PWD}
ORG_NAME=$1
shift

readonly SERVER_HOME="${WORKING_DIR}/server/${ORG_NAME}"
if [ ! -d ${SERVER_HOME} ]; then
    printf "The Fabric CA server for the organization specified does not exist. Try to initialize it first.\n"
    exit 3
fi

setServerEnvVars
cd ${SERVER_HOME}
fabric-ca-server start -b admin:adminpw
